| Path: | README |
| Last Update: | Mon Apr 28 20:18:42 -0400 2008 |
I dig acts_as_state_machine (elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/) and use it in almost all of my apps. The biggest thing that bugged me was:
When you call a method like object.event! it writes to the database, instead of updating the current state, which makes it hard to set a state before validation.
script/plugin install http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/ script/plugin install git://github.com/zilkey/acts_as_state_machine_hacks.git
This plugin hack adds new methods without the exclamation point - it just updates the attribute. So if you have:
class Conversation < ActiveRecord::Base
acts_as_state_machine :initial => :open
state :opened
state :closed
event :open do
transitions :to => :opened, :from => :closed
end
end
ActsAsStateMachine would give you:
conversation.open!
That uses update_attribute under the hood. This plugin adds:
conversation.open
Which just sets state to opened, without touching the database. Suitable for calling before you save, like:
def create
@conversation = Conversation.new(params[:conversation])
@conversation.open if params[:conversation][:publish] == "open"
if @conversation.save
...
else
...
end
end
def update
@conversation = Conversation.find(params[:id])
@conversation.open if params[:conversation][:publish] == "open"
if @conversation.update_attributes(params[:conversation]) # => if this doesn't validate, the state column will not have been written
...
else
...
end
end
I wrote this primarily because I often change state based on fields in the model - like if you check a "published" check box that changes a state from pending to published.
This also allows the possibility of adding a dropdown list to an admin screen where admins can set the status, while maintaining the integrity by still relying on events.
You must have the acts_as_state_machine plugin installed and the acts_as_state_machine directory must be named "acts_as_state_machine".
This copies methods directly from acts_as_state_machine. If acts_as_state_machine is updated, this plugin hack will likely break.
If you want to add your own hacks, you can fork this project on github.com/zilkey/acts_as_state_machine_hacks/tree/master
It includes working tests, which can be the hardest part of testing a plugin.